package com.project.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.project.enums.ServletActions;
import com.project.enums.ServletResult;

public abstract class AbstractServlet extends HttpServlet{

	private static final long serialVersionUID = 101839832904L;
	
	protected String doByType(HttpServletRequest req, String type) {
		String result = ServletResult.ERROR.toString();
		
		if (type.equalsIgnoreCase(ServletActions.LOGIN.toString())) {
			
			result = validate(req);
			
		} 
		
		if (type.equalsIgnoreCase(ServletActions.INSERT.toString())) {
			
			result = insert(req);
		}
		
		if (type.equalsIgnoreCase(ServletActions.UPDATE.toString())) {
			
			result = update(req);
		}
		
		if(type.equalsIgnoreCase(ServletActions.DISABLE.toString())) {
			
			result = disable(req);
		}
		
		if(type.equalsIgnoreCase(ServletActions.ENABLE.toString())) {
			
			result = enable(req);
		}
		
		if(type.equalsIgnoreCase(ServletActions.LOGOUT.toString())) {
			
			result = logout(req);
			
		}
		
		return result;
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");
		
		String result = doByType(request, type);
		
		response.setContentType("text/plain");
		response.getWriter().println(result);
	}
	
	protected abstract String validate(HttpServletRequest request);
	
	protected abstract String logout(HttpServletRequest request);
	
	protected abstract String insert(HttpServletRequest request);
	
	protected abstract String update(HttpServletRequest request);
	
	protected abstract String disable(HttpServletRequest request);
	
	protected abstract String enable(HttpServletRequest request);
	
}
